import {
  Controller,
  Get,
  Inject,
  Optional,
  Param,
  Query,
  SetMetadata,
  UseFilters,
  UseGuards,
  UseInterceptors,
  UsePipes,
  Headers,
  Header,
  Ip,
  Session,
  HostParam,
  Req,
  Res,
  Next,
  HttpCode,
  Redirect,
  Render,
  BadRequestException,
  DefaultValuePipe,
  Version,
  VERSION_NEUTRAL,
} from '@nestjs/common';
import { AppService } from './app.service';
import { LoginGuard } from './login.guard';
import { TimeInterceptor } from './time.interceptor';
import { ValidatePipe } from './validate.pipe';
import { TestFilter } from './test.filter';
import { Request, Response, NextFunction } from 'express';
import { Aaa } from './aaa.decorator';
import { My } from './my.decorator';
import { MyIp } from './param.decorator';
import { MyHeaders } from './MyHeaders.decorator';
import { MyQuery } from './MyQuery.decorator';
import { Ctro } from './ctro.decorator';
import { PersonService } from './person/person.service';
@Controller({
  path: '',
  version: VERSION_NEUTRAL, // VERSION_NEUTRAL 所有版本都可以访问
})
@SetMetadata('roles', ['user'])
export class AppController {
  constructor() {}

  @Inject(AppService)
  private readonly appService: AppService;

  @Inject(PersonService)
  private personService: PersonService;

  @Inject('person')
  private readonly person: {
    name: string;
    age: number;
  };

  @Optional() // 设置可选注入
  @Inject('person2')
  private readonly person2: {
    name: string;
    age: string;
  };

  // @Get('/ggg') // 设置路由
  // @UseGuards(LoginGuard) // 设置局部守卫
  @My('/ggg/:ids', 'admin123456') // 添加自定义装饰器 自定义了 @Get('/ggg') @Aaa('admin') @UseGuards(LoginGuard)
  @UseInterceptors(TimeInterceptor) // 设置局部拦截器
  @UseFilters(TestFilter) // 添加过滤器
  // @UsePipes(ValidatePipe) // 添加管道 更多的用法还是用于参数上
  // @SetMetadata('roles', ['admin'])
  // @Aaa('admin123')
  async getHello(
    @MyIp() ip: string,
    @Session() session: Record<string, any>,
    @MyHeaders('accept-language') al: string,
    @Headers('Accept') accept: string,
    @Headers() headers: Record<string, any>,
    @HostParam('host') host: string,
    @Req() req: Request,
    // @Res() res: Response,
    @MyQuery('id', new ValidatePipe()) id: number,
    @Param('ids') paramId: number,
    @MyQuery('name', new DefaultValuePipe('张三')) name: Record<string, any>,
  ): Promise<string> {
    // 如果你不会自己返回响应，可以通过 passthrough 参数告诉 Nest
    // @Res({passthrough :true}) res: Response
    // return 'hello world'
    // await new Promise((resolve) => setTimeout(resolve, 4000));
    // throw new BadRequestException('bad request');
    console.log('ip===', ip);
    console.log('accept-language===', al);
    // console.log('session===', session);
    console.log('handler...', id);
    console.log('name===', name);
    // console.log(this.person2);
    // console.log('headers===', headers);
    // console.log('host===', host);
    // console.log('req===', req);
    // console.log('res===', res);
    // return this.appService.getHello();
    // res.send(this.person2.name);
    // this.person2.name + id
    return id.toString() + '=====' + paramId.toString();
    // return this.appService.getHello() + this.personService.findOne(id);
  }

  // @Next 转发
  @Version('2') // 2.0.0 版本的接口
  @Get('/aaa')
  aaa(@Next() next: NextFunction, @Res() res: Response) {
    console.log('aaa');
    next();
    return '111';
  }
  @HttpCode(222) // 设置默认响应码
  @Header('aaa', 'bbb') // 设置头
  @Get('/aaa')
  @Redirect()
  // @Redirect('https://www.baidu.com') // @Redirect 装饰器来指定路由重定向的 url
  bbb() {
    console.log('bbb');
    return {
      url: 'https://www.baidu.com',
      statusCode: 302, // 默认302
    };
  }
  @Get('/user')
  @Render('user')
  user() {
    return {
      name: '张三ggg',
      age: 18,
    };
  }
}
